package com.wxg.controller;

import lombok.extern.java.Log;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.List;

@Log
@RestController
public class FileController {

    private final static String UPLOAD_FILE_PATH = "G:\\temp\\upload\\";

    @RequestMapping(value = "/upload")
    public String upload(@RequestParam("file") MultipartFile file) {
        if (file.isEmpty()) {
            return "file is empty";
        }
        String filename = file.getOriginalFilename();
        String suffixName = filename.substring(filename.lastIndexOf("."));
        log.info(String.format("上传的文件名为: %s 后缀名为 %s", filename, suffixName));
        String filePath = UPLOAD_FILE_PATH;
        File dest = new File(filePath + filename);
        if (!dest.getParentFile().exists()) {
            dest.getParentFile().mkdirs();
        }
        try {
            file.transferTo(dest);
        } catch (IOException e) {
            e.printStackTrace();
            return "upload failure";
        }
        return "upload success";
    }

    @PostMapping("/batch")
    public String handleMultiFileUpload(HttpServletRequest request) {
        List<MultipartFile> files
                = ((MultipartHttpServletRequest) request).getFiles("file");
        MultipartFile file = null;
        BufferedOutputStream stream = null;
        String filePath = UPLOAD_FILE_PATH;

        for (int i = 0; i < files.size(); i++) {
            file = files.get(i);
            if (!file.isEmpty()) {
                try {
                    byte[] bytes = file.getBytes();
                    stream = new BufferedOutputStream(
                            new FileOutputStream(
                                    new File(filePath
                                            + file.getOriginalFilename())));
                    stream.write(bytes);
                    stream.close();
                } catch (IOException e) {
                    stream = null;
                    return String.format("the %d file upload failure", i);
                }
            } else {
                return String.format("the %d file id empty", i);
            }
        }

        return "upload Multifile success";
    }


    @GetMapping("/download")
    public String downloadFile(HttpServletRequest request,
                               HttpServletResponse response) {

        File file = new File("C:\\Users\\Wxg\\Pictures\\太吾绘卷_人物创建001.png");
        String fileName = file.getName();
        response.setContentType("application/force-download");
        response.addHeader("Content-Disposition",
                String.format("attachment;fileName=%s", fileName));
        byte[] buffer = new byte[1024];
        FileInputStream fis = null;
        BufferedInputStream bis = null;
        try {
            fis = new FileInputStream(file);
            bis = new BufferedInputStream(fis);
            ServletOutputStream os = response.getOutputStream();
            int read = bis.read(buffer);
            while (read != -1) {
                os.write(buffer, 0, read);
                read = bis.read(buffer);
            }
            return "download success";
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                bis.close();
                fis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return "download failure";
    }

}
